package com.yjzx.util.common.util.batch.mergingLimit;

/**
 * @author guozhi
 * @date 2025/1/13
 * @description SlotManager
 */

import java.util.*;

public class MergingSlotManager<T, U extends Comparable<U>, R> implements SlotManager<MergingSlot<T, U, R>> {
    Set<MergingSlot<T, U, R>> mergingSlots;

    public MergingSlotManager() {
        this.mergingSlots = new TreeSet<>();
    }

    public MergingSlotManager(MergingSlot<T, U, R>[] newTimeMergingSlot) {
        this();
        for (MergingSlot<T, U, R> mergingSlot : newTimeMergingSlot) {
            addSlot(mergingSlot);
        }
    }

    @Override
    public Collection<MergingSlot<T, U, R>> getAllSlot() {
        return mergingSlots;
    }

    @Override
    public void addSlot(MergingSlot<T, U, R> newTimeMergingSlot) {
        // 检查新时间段是否与现有时间段重叠
        for (MergingSlot<T, U, R> existingTimeMergingSlot : mergingSlots) {
            if (newTimeMergingSlot.getStart().compareTo(existingTimeMergingSlot.getEnd()) <= 0 && newTimeMergingSlot.getEnd().compareTo(existingTimeMergingSlot.getStart()) >= 0) {
                throw new IllegalArgumentException("New time slot overlaps with existing time slot");
            }
        }
        // 添加新时间段并排序
        mergingSlots.add(newTimeMergingSlot); // 添加成功
    }


}
